package com.agritech.controller;

import com.agritech.common.Result;
import com.agritech.entity.User;
import com.agritech.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器，使用统一响应格式
 */
@RestController
@CrossOrigin
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public List<User> list() {
        return userService.getAllUsers();
    }


    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        boolean success = userService.register(user);
        System.out.println(user.getUsername()+user.getPassword());
        if (success) {
            System.out.println(user.getUsername()+"注册成功");
            return Result.ok("注册成功");
        } else {
            System.out.println(user.getUsername()+"用户名已存在");
            return Result.error("用户名已存在");
        }
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> loginInfo) {
        String username = loginInfo.get("username");
        String password = loginInfo.get("password");

        User user = userService.login(username, password);
//        System.out.println(user.getUsername()+user.getPassword());

        if (user != null) {
            // 生成Token（实际项目中应使用JWT）
            String token = generateToken(user);
            return Result.ok("登录成功", new HashMap<String, Object>() {{
                put("user", user);
                put("token", token);
            }});
        } else {
            return Result.error("用户名或密码错误");
        }
    }

    /**
     * 获取用户详情
     */
    @GetMapping("/{id}")
    public Result getUser(@PathVariable Long id) {
        User user = userService.getById(id);
        if (user != null) {
            return Result.ok(user);
        } else {
            return Result.error("用户不存在");
        }
    }

    /**
     * 获取所有用户
     */
    @GetMapping
    public Result getAllUsers() {
        List<User> users = userService.getAllUsers();
        return Result.ok(users);
    }

    /**
     * 更新用户信息
     */
    @PutMapping
    public Result updateUser(@RequestBody User user) {
        boolean success = userService.updateUser(user);
        if (success) {
            return Result.ok("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public Result deleteUser(@PathVariable Long id) {
        boolean success = userService.deleteUser(id);
        if (success) {
            return Result.ok("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    /**
     * 生成Token（示例方法）
     */
    private String generateToken(User user) {
        // 实际项目中应使用JWT生成
        return "token_" + user.getId() + "_" + System.currentTimeMillis();
    }
}